package com.huawei.testcloud.location;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by hasee-pc on 2017/7/22.
 */

public class MyService extends Service {
    private NotificationManager mNotificationManager;
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //获取到系统当前时间 long类型
            long time = System.currentTimeMillis();
            //将long类型的时间转换成日历格式
            Date data = new Date(time);
            // 转换格式，年月日时分秒 星期  的格式
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 EEE");
            Toast.makeText(MyService.this,simpleDateFormat.format(data),Toast.LENGTH_SHORT).show();
            //显示在textview上，通过转换格式
            //tvTime.setText(simpleDateFormat.format(data));

        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        new TimeThread().start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startForeground(1,createNotification(this));
        //mNotificationManager.notify(2,createNotification(this));
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public static Notification createNotification(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra("from_notification", true);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        Notification.Builder builder = new Notification.Builder(context)
                .setContentIntent(pendingIntent)
                .setContentTitle("测试")
                .setContentText("测试测试")
                .setSmallIcon(R.drawable.ic_notification);
                //.setLargeIcon(cover);
        Notification noti=builder.build();
        noti.flags = Notification.FLAG_ONGOING_EVENT;
        noti.flags|= Notification.FLAG_NO_CLEAR;
        noti.flags|= Notification.FLAG_FOREGROUND_SERVICE;
        return noti;
    }

    class TimeThread extends Thread{
        @Override
        public void run() {
            super.run();
            do {
                try {
                    //每隔一秒 发送一次消息
                    Thread.sleep(30000);
                    Message msg = new Message();
                    //消息内容 为MSG_ONE
                    msg.what = 1;
                    //发送
                    mHandler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);

        }
    }
}
